﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TableModule;

namespace CustomModule
{
    public class ConveyerManager
    {
        static GeneralLib.HiPerfTimer HiPerfTimer;
        static HalconDotNet.HImage hImage;
        public static List<ProductItem> productItems;
        //public static HalconDotNet.HWindow hWindowHandleBuffer;
        public static double CurrentPos
        {
            
            get 
            {
                if(FrameManager.MachineStatus.Is4Axis)
                    return CustomManager.zMotionCard.iCurrentPos[4];
                else
                    return CustomManager.zMotionCard.iCurrentPos[3];
            }
        }
        public static void Init()
        {
            HiPerfTimer = new GeneralLib.HiPerfTimer();
            HiPerfTimer.Start();
            hImage = new HalconDotNet.HImage("byte", 20000, 4000);
            productItems = new List<ProductItem>();

            //hWindowHandleBuffer = new HWindow(0, 0, 20000, 4000, 0, "buffer", "");
            //hWindowHandleBuffer.SetFont(strDefaultFontBuffer);
            //hWindowHandleBuffer.SetDraw("margin");
            //hWindowHandleBuffer.SetLineWidth(8);
           // iLineWidthBuffer = 8;

        }
        public static void ShowProduct(SmartDisplayControl.SmartDisplayControl smartDisplayControl)
        {
            smartDisplayControl.ShowImage(hImage,false);
            smartDisplayControl.DisplayLine(3500, 0, 3500, 20000, "green");
            smartDisplayControl.DisplayLine(0, 10000, 4000, 10000, "green");
            //hWindowHandleBuffer.DispImage(hImage);
            for (int i = productItems.Count - 1; i > -1; i--)
            {
                productItems[i].ShowItem(smartDisplayControl, CurrentPos);
            }
            try
            {
                double dMinPosX = TableManager.tableRunner.GetPosition("MinPos").dXPos;
                double dMaxPosX = TableManager.tableRunner.GetPosition("MaxPos").dXPos;
                double dDisplayPostionX1 = (dMinPosX) * 5 + 10000;
                double dDisplayPostionX2 = (dMaxPosX) * 5 + 10000;

                if (dDisplayPostionX1 > 500 && dDisplayPostionX1 < 20000)
                {
                    smartDisplayControl.DisplayLine(0, dDisplayPostionX1, 4000, dDisplayPostionX1, "orange");
                }
                if (dDisplayPostionX2 > 500 && dDisplayPostionX2 < 20000)
                {
                    smartDisplayControl.DisplayLine(0, dDisplayPostionX2, 4000, dDisplayPostionX2, "orange");
                }
            }
            catch
            { }
            if (productItems.Count > 0)
            {
                for (int i = productItems.Count-1; i >-1; i--)
                {
                    double dDisplayPostionX = (productItems[i].ImagePosX + CurrentPos - productItems[i].TrigerBeltPosX) * 5 + 10000;
                    if (dDisplayPostionX < -1000)
                        productItems.RemoveAt(i);
                    if (dDisplayPostionX > 20000)
                        productItems.RemoveAt(i);
                }
                
            }
            


        }
        public static void AddNewItem(ProductItem newItem)
        {
            lock (HiPerfTimer)
            {
                double dCurrentPosNewX = (newItem.ImagePosX + CurrentPos - newItem.TrigerBeltPosX);
                double dCurrentPosNewY = newItem.ImagePosY;
                for (int i=productItems.Count-1 ; i > -1; i--)
                {
                    double dCurrentPosX = (productItems[i].ImagePosX + CurrentPos - productItems[i].TrigerBeltPosX);
                    double dCurrentPosY = productItems[i].ImagePosY;

                    double dR = Math.Sqrt((dCurrentPosNewX - dCurrentPosX) * (dCurrentPosNewX - dCurrentPosX) + (dCurrentPosNewY - dCurrentPosY) * (dCurrentPosNewY - dCurrentPosY));

                    if (dR < 20)
                    {
                        newItem.AreadyGlue = productItems[i].AreadyGlue;
                        productItems.RemoveAt(i);
                        
                    }
                }
                productItems.Add(newItem);
                //ConveyerManager.productItems.OrderByDescending(p => p.TrigerBeltPosX);
            }
        }
    }
}
